package com.xhs.service.impl;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @desc: 使用Callable和Future创建线程，可以获取子线程返回的结果
 * @projectName: tools-parent
 * @author: xhs
 * @date: 2022/12/5 21:30
 * @version: JDK 1.8
 */
@Slf4j
public class Thread5ServiceImpl implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        log.info(">>>>>>>>>>>>>>使用Callable和Future创建线程,我是子线程:{},返回结果为100<<<<<<<<<<<<<<<<<<<<", Thread.currentThread().getName());
        return 100;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        log.info(">>>>>>>>>>>>>>使用Callable和Future创建线程,我是主线程:{}<<<<<<<<<<<<<<<<<<<<", Thread.currentThread().getName());
        Thread5ServiceImpl threadCallable = new Thread5ServiceImpl();
        FutureTask<Integer> integerFutureTask = new FutureTask<>(threadCallable);
        new Thread(integerFutureTask).start();
        Integer result = integerFutureTask.get();
        log.info(">>>>>>>>>>>>>>我是主线程：{}，拿到子线程的返回结果为：{}<<<<<<<<<<<<<<<<<<<<", Thread.currentThread().getName(), result);
    }
}
